package com.snopy.socket;


import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/**
 * @author snopy
 * @version 1.0.0
 * @classname UDPClient
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/11 0:57
 */
public class UDPClient {
    public static void main(String[] args) throws IOException {
        try {
            DatagramSocket socket = new DatagramSocket();
            Thread thread = new UDPClientThread(socket);
            thread.start();
        }catch (Exception e){

        }
    }
}
class UDPClientThread extends Thread {
    private DatagramSocket socket;

    public UDPClientThread(DatagramSocket t_socket) {
        socket = t_socket;
    }

    @Override
    public void run() {
        System.out.println("线程开始，打印该客户端的ip和端口");
        try {
            byte[] content = "hello".getBytes(StandardCharsets.UTF_8);

            DatagramPacket dp=new DatagramPacket(content,content.length, InetAddress.getByName("127.0.0.1"),8021);
            //建立数据包，声明长度，接收端主机，端口号
            socket.send(dp);//发送数据
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}